using UnityEngine;

public class BoundaryManager : MonoBehaviour
{
    [Header("边界设置")]
    public float leftBoundary = -8f;
    public float rightBoundary = 8f;
    public float topBoundary = 5f;
    public float bottomBoundary = -5f;
    
    [Header("调试显示")]
    public bool showBoundaries = true;
    public Color boundaryColor = Color.red;
    
    private static BoundaryManager instance;
    public static BoundaryManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<BoundaryManager>();
            }
            return instance;
        }
    }
    
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }
    
    void Start()
    {
        // 根据摄像机自动计算边界
        Camera mainCamera = Camera.main;
        if (mainCamera != null && mainCamera.orthographic)
        {
            float cameraHeight = mainCamera.orthographicSize;
            float cameraWidth = cameraHeight * mainCamera.aspect;
            
            leftBoundary = -cameraWidth;
            rightBoundary = cameraWidth;
            topBoundary = cameraHeight;
            bottomBoundary = -cameraHeight;
            
            Debug.Log($"边界已设置: 左={leftBoundary}, 右={rightBoundary}, 上={topBoundary}, 下={bottomBoundary}");
        }
    }
    
    /// <summary>
    /// 限制位置在边界内
    /// </summary>
    public Vector3 ClampToBoundary(Vector3 position)
    {
        position.x = Mathf.Clamp(position.x, leftBoundary, rightBoundary);
        position.y = Mathf.Clamp(position.y, bottomBoundary, topBoundary);
        return position;
    }
    
    /// <summary>
    /// 检查位置是否在边界内
    /// </summary>
    public bool IsWithinBoundary(Vector3 position)
    {
        return position.x >= leftBoundary && position.x <= rightBoundary &&
               position.y >= bottomBoundary && position.y <= topBoundary;
    }
    
    /// <summary>
    /// 检查对象是否超出边界（用于销毁飞出屏幕的对象）
    /// </summary>
    public bool IsOutOfBounds(Vector3 position, float margin = 1f)
    {
        return position.x < leftBoundary - margin || position.x > rightBoundary + margin ||
               position.y < bottomBoundary - margin || position.y > topBoundary + margin;
    }
    
    void OnDrawGizmos()
    {
        if (showBoundaries)
        {
            Gizmos.color = boundaryColor;
            
            // 绘制边界框
            Vector3 topLeft = new Vector3(leftBoundary, topBoundary, 0);
            Vector3 topRight = new Vector3(rightBoundary, topBoundary, 0);
            Vector3 bottomLeft = new Vector3(leftBoundary, bottomBoundary, 0);
            Vector3 bottomRight = new Vector3(rightBoundary, bottomBoundary, 0);
            
            Gizmos.DrawLine(topLeft, topRight);
            Gizmos.DrawLine(topRight, bottomRight);
            Gizmos.DrawLine(bottomRight, bottomLeft);
            Gizmos.DrawLine(bottomLeft, topLeft);
        }
    }
}